<template>
  <CompOperate
    :field="field"
    :designer="designer"
    :design-state="designState"
    :parent-list="parentList"
    :parent-widget="parentWidget"
    :sub-form-row-id="subFormRowId"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :index-of-parent-list="indexOfParentList"
    :display-style="field['options'].displayStyle"
  >
    <div class="scene-waiting-blue2-widget" :style="getStyle('area')">

      <div class="widget-inside" :class="{'header-hide':!optionsInfo['headShow']}">
        <!--列表头部-->
        <div class="list-head" :style="getStyle('head')" v-if="optionsInfo['headShow']">
          <template v-for="(item) in optionsInfo['colOptions']">
            <div class="label-item" :class="getClassName(item.value)" v-if="getColShow(item.value)"
                 :style="setFlex(item,'header')">
              <div class="item-inside">
                {{ item.label }}
              </div>
            </div>
          </template>
        </div>
        <!--列表内容-->
        <div class="list-body" v-if="true">
          <template v-for="(item,index) in (waitingPatientInfos)">
            <div class="list-body-inside" :style="getStyle('listItem')">
              <template v-for="(items) in (optionsInfo['colOptions'])">
                <template v-if="getColShow(items.value)">
                  <div class="label-item" :class="getClassName(items.value)" :style="setFlex(items,'list')">
                    <template v-if="(items.value!=='treatingPatient')&&(items.value!=='waitPatientList')">
                      <template v-if="(items.value==='waitPatientCount')">
                        <div class="item-inside">
                          {{ item[items.value] || '\u00a0' }}
                        </div>
                      </template>
                      <!--除了人数-->
                      <template v-else>
                        <div class="item-inside">
                          <ScrollX
                            :key="`${item[items.value]}-${index}`"
                            :speed="optionsInfo['listSpeed'] || 10"
                          >
                            {{ getColContent(item, (items.value)) }}
                          </ScrollX>
                        </div>
                      </template>
                    </template>
                    <!--就诊患者-->
                    <template v-if="items.value==='treatingPatient'">
                      <div class="item-inside">
                        <ScrollEditX :content="getCurrentPatient(item) || ''" :key="`${item[items.value]}`"/>
                      </div>
                    </template>
                    <!--候诊患者-->
                    <template v-if="items.value==='waitPatientList'">
                      <div class="item-inside col-wait-inside">
                        <div v-for="(item, index) in getWaitPatient(item.waitPatientList)" :key="index"
                             class="wait-info">
                          {{ item.orderNo }}&nbsp;{{ item.patientName || '\u00a0' }}
                        </div>
                      </div>
                    </template>
                  </div>
                </template>
              </template>
            </div>
          </template>

        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from 'uuid';
import {pxToVw} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import vueSeamlessScroll from 'vue-seamless-scroll';
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import ScrollEditX from '@c/public/seamlessScroll/ScrollEditX.vue'
import {buildParentApi} from "@/utils/designer/util";
import ScrollEditY from "@c/public/seamlessScroll/ScrollEditY.vue";
import * as types from "@c/public/form-designer/devType/datas-types";

let buildParent = buildParentApi();

export default {
  name: "scene-waiting-blue2-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  components: {
    ScrollEditY,
    ScrollX,
    ScrollEditX,
    CompOperate,
    vueSeamlessScroll,
  },

  data() {
    return {
      widgetKey: uuid(),
      optionsInfo: Object(),

      rowFieldsDict: [
        {key: 'dept', field: 'deptShowName'},
        {key: 'room', field: 'roomShowName'},
        {key: 'number', field: 'numberName'},
        {key: 'doc', field: 'docShowName'},
        {key: 'curr', field: 'currShowName'},
        {key: 'wait', field: 'waitShowName'},
        {key: 'total', field: 'total'}
      ]
    }
  },


  computed: {
    //生成唯一的字符串
    getOnlyId() {
      return function () {
        return uuid()
      }
    },

    // 模板的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Object();
        let Obj = Object();
        switch (val) {
          case "area":
            Reflect.set(Obj, "height", pxToVw(info.contentHeight));
            return Obj;
          case "head":
            Reflect.set(Obj, "color", (info.headerFontColor));
            Reflect.set(Obj, "fontWeight", (info.headFontWeight));
            return Obj;
          case "headItem":
            Reflect.set(Obj, "fontSize", pxToVw(info.headFontSize));
            Reflect.set(Obj, "backgroundColor", (info.headerBackgroundColor));
            return Obj;
          case "list":
            Reflect.set(Obj, "height", pxToVw(info.contentHeight));
            return Obj;
          case "listItem":
            Reflect.set(Obj, "fontSize", pxToVw(info.itemFontSize));
            Reflect.set(Obj, "fontWeight", (info.itemFontWeight));
            return Obj;
        }
      };
    },

    // 设置列宽
    setFlex() {
      return function (item, val) {
        let Obj = Object();
        let info = (this.optionsInfo) || Object();
        switch (val) {
          case "header":
            Reflect.set(Obj, "flex", (item.flex) || 1);
            Reflect.set(Obj, "fontSize", pxToVw(info.headFontSize));
            Reflect.set(Obj, "backgroundColor", (info.headerBackgroundColor));
            return Obj;
          case "list":
            Reflect.set(Obj, "flex", (item.flex) || 1);
            return Obj;
        }
      }
    },

    // 栏目是否显示
    getColShow() {
      return function (val) {
        const self = this;
        let { defaultShowCol, mergeColInfo } = self.optionsInfo || Array();
        if (val === "registerObjNames") {
          if (
            !!mergeColInfo &&
            !!mergeColInfo[1] &&
            !!mergeColInfo[1].length &&
            mergeColInfo[1].length === 2
          ) {
            return false;
          } else {
            return defaultShowCol.includes(val);
          }
        } else {
          return defaultShowCol.includes(val);
        }
      };
    },

    // 患者的ClassName
    getClassName() {
      return function (val) {
        switch (val) {
          case "treatingPatient":
            return "current-patient";
          case "waitPatientList":
            return "wait-patient";
          case "waitPatientCount":
            return "patient-count";
          case "registerObjNames":
            return "register-name";
          case 'deptName':
            return "deptName-name";
        }
      };
    },

    // 拼接显示具体内容
    getColContent() {
      return function (val, key) {
        const self = this;
        let {mergeColInfo, registerObjNameKey, roomNameKey,deptNameKey} = (self.optionsInfo) || Object();
        switch (key) {
          // 诊室&诊室简称
          case 'treatmentRoomName':
            if (!!mergeColInfo && !!mergeColInfo[1] && !!(mergeColInfo[1].length) && (mergeColInfo[1].length) === 2) {
              let mergeInfo = `${val[roomNameKey] || val[key] || '\u00a0'} ${val[registerObjNameKey] || '\u00a0'}`
              return self.$lodash.trim(mergeInfo);
            } else {
              return val[roomNameKey] || val[key] || '\u00a0'
            }
          case 'registerObjNames':
            return val[registerObjNameKey] || val[key];
          case 'deptName':
            return val[deptNameKey] || val[key];
          default:
            return val[key] || '\u00a0'
        }
      }
    },

    // 患者列表
    waitingPatientInfos() {
      let devType = (this.devType);
      let {showRowNums = 5} = (this.optionsInfo);
      let WaitingPatientList = this.$store.getters['getWaitingPatientInfo'];
      if (devType) {
        if (WaitingPatientList.apiData.length > showRowNums) {
          return WaitingPatientList.apiData.slice(0, showRowNums);
        } else {
          let arr = (new Array(showRowNums - WaitingPatientList.apiData.length).fill({}));
          return [...WaitingPatientList.apiData, ...arr];
        }
      } else {
        if (WaitingPatientList.mockData.length > showRowNums) {
          return WaitingPatientList.mockData.slice(0, showRowNums);
        } else {
          let arr = (new Array(showRowNums - WaitingPatientList.mockData.length).fill({}));
          return [...WaitingPatientList.mockData, ...arr];
        }
      }
    },

    // 就诊的患者|| 医生暂停接诊
    getCurrentPatient() {
      return function (val) {
        /**
         * 在线状态 0在线 1暂停 2退出
         */
        let type = parseInt(val.onlineStatus);
        switch (type) {
          case 0:
          case 2:
            if (!!(val.treatingPatient) && !!(val.treatingPatient.length)) {
              let currentPatient = Array();
              (val.treatingPatient).forEach((item) => {
                let temp = `<span class="patient-orderNo">${(item.orderNo) || '\u00a0'}&nbsp;</span>${(item.patientName) || '\u00a0'}`;
                let temps = this.$lodash.trim(temp);
                currentPatient.push(temps)
              })
              let info = this.$lodash.join(currentPatient, ' ')
              return (this.$lodash.trim(info));
            } else {
              return '\u00a0'
            }
          case 1:
            return '暂停接诊';
        }
      }
    },

    //候诊患者--此场景固定截取1名候诊患者
    getWaitPatient() {
      return function (row) {
        if (!!row&&Array.isArray(row)) {
          let sliceInfo = row.slice(0, 1);
          if (sliceInfo.length === 1) {
            return sliceInfo
          } else {
            let arr = (new Array(1 - sliceInfo.length).fill({}));
            return [...sliceInfo, ...arr]
          }
        } else {
          return new Array(1).fill({});
        }
      }
    }
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 等待候诊滚动结束
    handleScrollEnd(e, row) {
      const self = this;
      if (self.$refs[`scroll-${row.workerId}`]) {
        self.$nextTick(() => {
          self.$refs[`scroll-${row.workerId}`][0] && self.$refs[`scroll-${row.workerId}`][0].reset()
        })
      }
    },

    // 接收父组件传递过来的数据
    async executeMethod(key, data) {
      switch (key) {
        case "patientListInfo":
          let patientList = this.$lodash.cloneDeep(data);
          // buildParent.executeParentMethod('writeLog', JSON.stringify({sign: '模板-患者信息', key: key, data: data}));
          await this.$store.dispatch('setWaitingPatientInfo', patientList)
          break
      }
    }
  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = (val.options.config);
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-waiting-blue2-widget {
  color: #333;
  align-self: center;
  align-items: center;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  backdrop-filter: blur(4px);
  margin: 20px 24px 0 24px;
  box-sizing: border-box;

  //不隐藏头部
  .widget-inside {
    width: 100%;
    height: 100%;
    overflow: hidden;
    padding: 20px 12px 6px 12px;
    box-sizing: border-box;
    margin-bottom: 40px;
    border-radius: 20px !important;

    .list-head,
    .list-body {
      width: 100%;
      background: transparent !important;
    }

    .list-head {
      display: flex;
      margin-bottom: 4px;

      .label-item {
        display: flex;
        flex: 1;
        height: 98px;
        line-height: 98px;
        text-align: center;
        border-right: 1px solid rgba(1, 204, 253, 1);
        border-bottom: 1px solid rgba(1, 204, 253, 1);
        overflow: hidden;
        align-self: center;
        align-items: center;

        .item-inside {
          width: 100%;
          text-align: center;
          @include singleEllipsis(100%)
        }
      }

      .label-item:last-child {
        border-right: none;
      }

    }

    .list-body {
      display: flex;
      width: 100%;
      height: calc(100% - 110px);
      align-items: center;
      align-self: center;
      flex-direction: column;
      flex-grow: 1;

      .list-body-inside {
        display: flex;
        flex-direction: row;
        width: 100%;
        flex-grow: 1;
        box-sizing: border-box;
        border-bottom: 1px solid rgba(1, 204, 253, 1);

        .label-item {
          border-right: 1px solid rgba(1, 204, 253, 1);
          color: white;
          flex: 1;
          height: auto;
          line-height: 100%;
          text-align: center;
          width: 100%;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          display: inline-flex;
          align-self: auto;
          align-content: center;

          .item-inside {
            width: calc(100% - 24px);
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            display: inline-block;
            padding: 0 12px;
            box-sizing: content-box;
            align-self: center;
          }

          .col-wait-inside {
            display: flex;
            height: 100%;
            background: transparent;

            .wait-info {
              flex: 1;
              height: 100%;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
              border-radius: 4px;
              display: inline-flex;
              text-align: center;
              align-items: center;
              justify-content: center;

            }
          }
        }

        .deptName-name {
          color: rgba(148, 247, 252, 1);
        }

        .label-item:last-child {
          border-right: none;
        }
      }


      .list-body-inside:last-child {
        border-bottom: none;
      }
    }
  }

  //隐藏头部--列表高度的变化
  .header-hide {
    .list-body {
      height: calc(100%);
    }
  }


}
</style>

